package com.ysu.zyw.controller;

import com.ysu.zyw.other.exception.KnownException;
import com.ysu.zyw.service.BlogArticleService;
import com.ysu.zyw.service.BlogMessageService;
import com.ysu.zyw.service.BlogPromptService;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by zhang_yao_wu1993 on 15/4/27.
 *
 * @author by Zerur
 */
@Controller
public class BlogArticleController {
    private BlogArticleService blogArticleService;
    private BlogPromptService blogPromptService;

    public BlogPromptService getBlogPromptService() {
        return blogPromptService;
    }

    @Resource(name = "blogPromptService")
    public void setBlogPromptService(BlogPromptService blogPromptService) {
        this.blogPromptService = blogPromptService;
    }


    public BlogArticleService getBlogArticleService() {
        return blogArticleService;
    }

    @Resource(name = "blogArticleService")
    public void setBlogArticleService(BlogArticleService blogArticleService) {
        this.blogArticleService = blogArticleService;
    }



    @RequestMapping("/DeleteArticle/{uuid}")
    public String deleteArticle(@PathVariable String uuid, HttpServletRequest request) {
        blogArticleService.deleteArticle(uuid, request);
        return "redirect:/ManagePage";
    }


    @ExceptionHandler(AccessDeniedException.class)
    public ModelAndView handlerAccessDeniedException(AccessDeniedException e) {
        e.printStackTrace();
        ModelAndView modelAndView = new ModelAndView("/WEB-INF/view/blog_Prompt.jsp");
        blogPromptService.fillInsufficientPermissionsModelAndView(modelAndView);
        return modelAndView;
    }

    @ExceptionHandler(KnownException.class)
    public ModelAndView handlerKnownException(KnownException e) {
        e.printStackTrace();
        ModelAndView modelAndView = new ModelAndView("/WEB-INF/view/blog_Prompt.jsp");
        blogPromptService.fillKnownExceptionPromptModelAndView(modelAndView, e);
        return modelAndView;
    }

    @ExceptionHandler(Exception.class)
    public ModelAndView handlerUnknownException(Exception e) {
        e.printStackTrace();
        ModelAndView modelAndView = new ModelAndView("/WEB-INF/view/blog_Prompt.jsp");
        blogPromptService.fillUnknownExceptionPromptModelAndView(modelAndView);
        return modelAndView;
    }
}
